home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
TEMP
/
GNU
/
flex
/
Examples
< prev
next >
Wrap
Text File
|
1995-06-28
|
3KB
|
121 lines
Examples
Previous: <Description=>Descriptio> * Next: <Format=>Format> * Up: <Top=>!Root>
#Wrap on
{fH3}Some simple examples{f}
First some simple examples to get the flavor of how one
uses {fCode}flex{f}. The following {fCode}flex{f} input specifies a scanner
which whenever it encounters the string "username" will
replace it with the user's login name:
#Wrap off
#fCode
%%
username printf( "%s", getlogin() );
#f
#Wrap on
By default, any text not matched by a {fCode}flex{f} scanner is
copied to the output, so the net effect of this scanner is
to copy its input file to its output with each occurrence
of "username" expanded. In this input, there is just one
rule. "username" is the {fStrong}pattern{f} and the "printf" is the
{fStrong}action{f}. The "%%" marks the beginning of the rules.
Here's another simple example:
#Wrap off
#fCode
int num\_lines = 0, num\_chars = 0;
%%
\\n ++num\_lines; ++num\_chars;
. ++num\_chars;
%%
main()
\{
yylex();
printf( "\# of lines = %d, \# of chars = %d\\n",
num\_lines, num\_chars );
\}
#f
#Wrap on
This scanner counts the number of characters and the
number of lines in its input (it produces no output other
than the final report on the counts). The first line
declares two globals, "num\_lines" and "num\_chars", which
are accessible both inside {fEmphasis}yylex(){f} and in the {fEmphasis}main(){f}
routine declared after the second "%%". There are two rules,
one which matches a newline ("\\n") and increments both the
line count and the character count, and one which matches
any character other than a newline (indicated by the "."
regular expression).
A somewhat more complicated example:
#Wrap off
#fCode
\/\* scanner for a toy Pascal-like language \*\/
%\{
\/\* need this for the call to atof() below \*\/
\#include <math.h>
%\}
DIGIT [0-9]
ID [a-z][a-z0-9]\*
%%
\{DIGIT\}+ \{
printf( "An integer: %s (%d)\\n", yytext,
atoi( yytext ) );
\}
\{DIGIT\}+"."\{DIGIT\}\* \{
printf( "A float: %s (%g)\\n", yytext,
atof( yytext ) );
\}
if|then|begin|end|procedure|function \{
printf( "A keyword: %s\\n", yytext );
\}
\{ID\} printf( "An identifier: %s\\n", yytext );
"+"|"-"|"\*"|"\/" printf( "An operator: %s\\n", yytext );
"\{"[^\}\\n]\*"\}" \/\* eat up one-line comments \*\/
[ \\t\\n]+ \/\* eat up whitespace \*\/
. printf( "Unrecognized character: %s\\n", yytext );
%%
main( argc, argv )
int argc;
char \*\*argv;
\{
++argv, --argc; \/\* skip over program name \*\/
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
\}
#f
#Wrap on
This is the beginnings of a simple scanner for a language
like Pascal. It identifies different types of {fStrong}tokens{f} and
reports on what it has seen.
The details of this example will be explained in the
following sections.